/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : SysUart.c
  * @brief          : 串口操作文件
  ******************************************************************************
  ** 本文件用于实现串口操作的程序
  *
  ******************************************************************************
  */
/* USER CODE END Header */



/* USER CODE BEGIN Includes */

#include "SysUart.h"
#include "Config.h"

/* USER CODE END Includes */


/* USER CODE BEGIN Variables */



tUartRxBufDef Uart1RxBuf;
tUartRxBufDef Uart2RxBuf;
//tUartRxBufDef Uart3RxBuf;
//tUartRxBufDef Uart4RxBuf;
//tUartRxBufDef Uart5RxBuf;

/* USER CODE END Variables */

/*******************************************************************************************************/

/* USER CODE BEGIN  Function prototypes */



void UartInit(UART_HandleTypeDef *huart,uint32_t bps,uint16_t pay)
{

	__HAL_UART_DISABLE(huart);
	
	//波特率
	if((1200==bps)||
			(2400==bps)||
			(4800==bps)||
			(9600==bps)||
			(14400==bps)||
			(19200==bps)||
			(38400==bps)||
			(56000==bps)||
			(57600==bps)||
			(115200==bps)
		)
		huart->Init.BaudRate = bps;
	else	huart->Init.BaudRate = 115200;
	
	//数据位
	if(0==pay)
		huart->Init.WordLength = UART_WORDLENGTH_8B;
	else
		huart->Init.WordLength = UART_WORDLENGTH_9B;
	
	//校验位
	if(0==pay)
		huart->Init.Parity = UART_PARITY_NONE;
	else if(1==pay)
		huart->Init.Parity = UART_PARITY_EVEN;
	else
		huart->Init.Parity = UART_PARITY_ODD;
	
	//其他默认参数
  huart->Init.Mode = UART_MODE_TX_RX;
  huart->Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart->Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(huart) != HAL_OK)
  {
    Error_Handler();
  }
	__HAL_UART_ENABLE(huart);

}



void UartBufInit(UART_HandleTypeDef *huart,tUartRxBufDef *UartRxBuf)
{

		UartRxBuf->Size=huart->RxXferSize-huart->RxXferCount;
		UartRxBuf->Start=0;
		
		//启动串口中断
		HAL_UART_Receive_IT(huart,UartRxBuf->Buf,UART_RXSIZE);
		
		
		//启动总线空闲中断
		__HAL_UART_ENABLE_IT(huart,UART_IT_IDLE);
	if(&huart1==huart)
	{
		//RS485 ENABLE
//		HAL_GPIO_WritePin(UART1_RE_GPIO_Port,UART1_RE_Pin,GPIO_PIN_RESET);
	}
	if(&huart2==huart)
	{
		//RS485 ENABLE
//		HAL_GPIO_WritePin(UART1_RE_GPIO_Port,UART1_RE_Pin,GPIO_PIN_RESET);
	}
}


void UartSend(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
{
	if(&huart1==huart)
	{
		
//		HAL_GPIO_WritePin(UART1_RE_GPIO_Port,UART1_RE_Pin,GPIO_PIN_SET);
	}
	if(&huart2==huart)
	{
//		HAL_GPIO_WritePin(UART2_DE_GPIO_Port,UART2_DE_Pin,GPIO_PIN_SET);
	}
	HAL_UART_Transmit_IT(huart, pData, Size);
}

void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
	SetDataConfig->ModemSet.Save=1;
	if(&huart1==huart)
	{
		
//		HAL_GPIO_WritePin(UART1_RE_GPIO_Port,UART1_RE_Pin,GPIO_PIN_RESET);
	}
	if(&huart2==huart)
	{
//		HAL_GPIO_WritePin(UART2_DE_GPIO_Port,UART2_DE_Pin,GPIO_PIN_RESET);
	}
}

//串口数据接收回调
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	if(&huart1==huart)
	{
		Uart1RxBuf.Size=huart->RxXferSize-huart->RxXferCount;
		if(0<Uart1RxBuf.Size)
			Uart1RxBuf.Start=1;

		HAL_UART_Receive_IT(huart,Uart1RxBuf.Buf,UART_RXSIZE);
	}
	if(&huart2==huart)
	{
		Uart2RxBuf.Size=huart->RxXferSize-huart->RxXferCount;
		if(0<Uart2RxBuf.Size)
			Uart2RxBuf.Start=1;

		HAL_UART_Receive_IT(huart,Uart2RxBuf.Buf,UART_RXSIZE);
	}
//	if(&huart3==huart)
//	{
//		Uart3RxBuf.Size=huart->RxXferSize-huart->RxXferCount;
//		if(0<Uart3RxBuf.Size)
//			Uart3RxBuf.Start=1;

//		HAL_UART_Receive_IT(huart,Uart3RxBuf.Buf,UART_RXSIZE);
//	}
//	if(&huart4==huart)
//	{
//		Uart4RxBuf.Size=huart->RxXferSize-huart->RxXferCount;
//		if(0<Uart4RxBuf.Size)
//			Uart4RxBuf.Start=1;

//		HAL_UART_Receive_IT(huart,Uart4RxBuf.Buf,UART_RXSIZE);
//	}
//	if(&huart5==huart)
//	{
//		Uart5RxBuf.Size=huart->RxXferSize-huart->RxXferCount;
//		if(0<Uart5RxBuf.Size)
//			Uart5RxBuf.Start=1;

//		HAL_UART_Receive_IT(huart,Uart5RxBuf.Buf,UART_RXSIZE);
//	}

}

void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
{

	if(huart->Instance == USART1)
	{
			__HAL_UNLOCK(&huart1);
			HAL_UART_Receive_IT(&huart1, (uint8_t *)&Uart1RxBuf.Buf, 1);   //
	}
	if(huart->Instance == USART2)
	{
			__HAL_UNLOCK(&huart2);
			HAL_UART_Receive_IT(&huart2, (uint8_t *)&Uart2RxBuf.Buf, 1);   //
	}
//	if(huart->Instance == USART3)
//	{
//			__HAL_UNLOCK(&huart3);
//			HAL_UART_Receive_IT(&huart3, (uint8_t *)&Uart3RxBuf.Buf, 1);   //
//	}
//	if(huart->Instance == USART4)
//	{
//			__HAL_UNLOCK(&huart4);
//			HAL_UART_Receive_IT(&huart4, (uint8_t *)&Uart4RxBuf.Buf, 1);   //
//	}
//	if(huart->Instance == USART5)
//	{
//			__HAL_UNLOCK(&huart5);
//			HAL_UART_Receive_IT(&huart5, (uint8_t *)&Uart5RxBuf.Buf, 1);   //
//	}
}




/* USER CODE END  Function prototypes */


/*******************************************************************************************************/






/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
